﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Common
{
    public static class BitHelper
    {
        public static byte set_bit(this byte data, int index, bool flag)
        {
            if (index > 8 || index < 1)
                throw new ArgumentOutOfRangeException();

            int v = index < 2 ? index : (2 << (index - 2));

            return flag ? (byte)(data | v) : (byte)(data & ~v);
            //data = flag ? (byte)(data | v) : (byte)(data & ~v);
        }


        private static int GetbitValue(this byte input, int index)
        {
            if (index > sizeof(byte))
            {
                return -1;
            }
            //左移到最高位
            int value = input << (sizeof(byte) - 1 - index);
            //右移到最低位
            value = value >> (sizeof(byte) - 1);
            return value;
        }

    }
}
